package com.bosssoft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibFiscalDTO;
import com.bosssoft.entity.po.FibFiscalPO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.entity.po.FibFiscalPO;
import com.bosssoft.mapper.FibFiscalMapper;
import com.bosssoft.service.FibFiscalService;

import javax.annotation.Resource;

/**
 * @author Li kexi
 * Created on 2021/8/16.14:30
 */
@Service("fibFiscalService")
@Slf4j
public class FibFiscalServiceImpl extends ServiceImpl<FibFiscalMapper, FibFiscalPO> implements FibFiscalService {
    @Resource
    private FibFiscalMapper fibFiscalMapper;

    @Override
    public FibFiscalDTO selectOneById(Long id) {
        try {
            FibFiscalPO fibFiscalPO = this.fibFiscalMapper.selectById(id);
            FibFiscalDTO fibFiscalDTO = new FibFiscalDTO();
            BeanUtils.copyProperties(fibFiscalPO, fibFiscalDTO);
            return fibFiscalDTO;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public PageResult<FibFiscalPO> page(Page<FibFiscalPO> page, FibFiscalPO fibFiscalPO) {
        // 构造条件
        QueryWrapper<FibFiscalPO> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(fibFiscalPO.getFisId())) {
            queryWrapper.like("fis_id", fibFiscalPO.getFisId());
        }
        if (ObjectUtil.isNotNull(fibFiscalPO.getFisName())) {
            queryWrapper.like("fis_name", fibFiscalPO.getFisName());
        }
        // 查询分页结果
        return new PageResult<>(this.page(page, queryWrapper));
    }

    @Override
    public boolean selectDeleted(Long id) {
        try {
            return fibFiscalMapper.selectDeleted(id) > 0;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public List<FibFiscalDTO> selectAll(Long page, Long limit) {
        try {
            Long offset = page * 10 - 10;
            if (limit == null) {
                limit = Long.valueOf(10);
            }
            List<FibFiscalPO> fibFiscalPOS = this.fibFiscalMapper.selectAll(offset, limit);
            List<FibFiscalDTO> fibFiscalDTOS = new ArrayList<>();
            for (int i = 0; i < fibFiscalPOS.size(); i++) {
                fibFiscalDTOS.add(new FibFiscalDTO());
                BeanUtils.copyProperties(fibFiscalPOS.get(i), fibFiscalDTOS.get(i));
            }
            return fibFiscalDTOS;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public boolean insert(FibFiscalDTO fibFiscalDTO) {
        try {
            FibFiscalPO fibFiscalPO = new FibFiscalPO();
            BeanUtils.copyProperties(fibFiscalDTO, fibFiscalPO);
            return this.fibFiscalMapper.insert(fibFiscalPO) > 0;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public boolean delete(FibFiscalDTO fibFiscalDTO) {
        FibFiscalPO fibFiscalPO = new FibFiscalPO();
        BeanUtils.copyProperties(fibFiscalDTO, fibFiscalPO);
        return this.fibFiscalMapper.delete(fibFiscalPO);
    }

    @Override
    public boolean update(FibFiscalDTO fibFiscalDTO) {
        FibFiscalPO fibFiscalPO = new FibFiscalPO();
        BeanUtils.copyProperties(fibFiscalDTO, fibFiscalPO);
        return this.fibFiscalMapper.update(fibFiscalPO);
    }

    @Override
    public boolean updateDeleted(FibFiscalDTO fibFiscalDTO) {
        FibFiscalPO fibFiscalPO = new FibFiscalPO();
        BeanUtils.copyProperties(fibFiscalDTO, fibFiscalPO);
        return this.fibFiscalMapper.updateDeleted(fibFiscalPO);
    }
}
